<rd-header>
  <rd-header-title title-text="Team details"></rd-header-title>
  <rd-header-content>
    <a ui-sref="portainer.teams">Teams</a> &gt; <a ui-sref="portainer.teams.team({id: team.Id})">{{ team.Name }}</a>
  </rd-header-content>
</rd-header>

<div class="row">
  <div class="col-lg-12 col-md-12 col-xs-12">
    <rd-widget>
      <rd-widget-header icon="fa-users" title-text="Team details"></rd-widget-header>
      <rd-widget-body classes="no-padding">
        <table class="table">
          <tbody>
            <tr>
              <td>Name</td>
              <td>
                {{ team.Name }}
                <button class="btn btn-xs btn-danger" ng-if="isAdmin" ng-click="deleteTeam()"
                  ><i class="fa fa-trash-alt space-right" aria-hidden="true"></i>Delete this team</button
                >
              </td>
            </tr>
            <tr>
              <td>Leaders</td>
              <td>{{ leaderCount }}</td>
            </tr>
            <tr>
              <td>Total users in team</td>
              <td>{{ teamMembers.length }}</td>
            </tr>
          </tbody>
        </table>
      </rd-widget-body>
    </rd-widget>
  </div>
</div>

<div class="row" ng-if="team">
  <div class="col-sm-6">
    <rd-widget>
      <rd-widget-header classes="col-sm-12 col-md-6 nopadding" icon="fa-users" title-text="Users">
        <div class="pull-md-right pull-lg-right">
          Items per page:
          <select ng-model="state.pagination_count_users" ng-change="changePaginationCountUsers()">
            <option value="0">All</option>
            <option value="10">10</option>
            <option value="25">25</option>
            <option value="50">50</option>
            <option value="100">100</option>
          </select>
        </div>
      </rd-widget-header>
      <rd-widget-taskbar classes="col-sm-12 nopadding">
        <div class="col-sm-12 col-md-6 nopadding">
          <button class="btn btn-primary btn-sm" ng-click="addAllUsers()" ng-if="isAdmin" ng-disabled="users.length ===  0 || filteredUsers.length === 0"
            ><i class="fa fa-user-plus space-right" aria-hidden="true"></i>Add all users</button
          >
        </div>
        <div class="col-sm-12 col-md-6 nopadding">
          <input type="text" id="filter-users" ng-model="state.filterUsers" placeholder="Filter..." class="form-control input-sm" />
        </div>
      </rd-widget-taskbar>
      <rd-widget-body classes="no-padding">
        <div class="table-responsive">
          <table class="table table-hover">
            <thead>
              <tr>
                <th>
                  <a ng-click="orderUsers('Username')">
                    Name
                    <span ng-show="sortTypeUsers == 'Username' && !sortReverseUsers" class="glyphicon glyphicon-chevron-down"></span>
                    <span ng-show="sortTypeUsers == 'Username' && sortReverseUsers" class="glyphicon glyphicon-chevron-up"></span>
                  </a>
                </th>
              </tr>
            </thead>
            <tbody>
              <tr
                pagination-id="table1"
                dir-paginate="user in users | filter:state.filterUsers | orderBy:sortTypeUsers:sortReverseUsers | itemsPerPage: state.pagination_count_users"
              >
                <td>
                  {{ user.Username }}
                  <span style="margin-left: 5px">
                    <a ng-click="addUser(user)"><i class="fa fa-plus-circle space-right" aria-hidden="true"></i>Add</a>
                  </span>
                </td>
              </tr>
              <tr ng-if="!users">
                <td colspan="2" class="text-center text-muted">Loading...</td>
              </tr>
              <tr ng-if="users.length === 0 || (users | filter: state.filterUsers).length === 0">
                <td colspan="2" class="text-center text-muted">No users.</td>
              </tr>
            </tbody>
          </table>
          <div ng-if="users" class="pull-left pagination-controls">
            <dir-pagination-controls pagination-id="table1"></dir-pagination-controls>
          </div>
        </div>
      </rd-widget-body>
    </rd-widget>
  </div>
  <div class="col-sm-6">
    <rd-widget>
      <rd-widget-header classes="col-sm-12 col-md-6 nopadding" icon="fa-users" title-text="Team members">
        <div class="pull-md-right pull-lg-right">
          Items per page:
          <select ng-model="state.pagination_count_members" ng-change="changePaginationCountGroupMembers()">
            <option value="0">All</option>
            <option value="10">10</option>
            <option value="25">25</option>
            <option value="50">50</option>
            <option value="100">100</option>
          </select>
        </div>
      </rd-widget-header>
      <rd-widget-taskbar classes="col-sm-12 nopadding">
        <div class="col-sm-12 col-md-6 nopadding">
          <button class="btn btn-primary btn-sm" ng-click="removeAllUsers()" ng-if="isAdmin" ng-disabled="teamMembers.length ===  0 || filteredGroupMembers.length === 0"
            ><i class="fa fa-user-times space-right" aria-hidden="true"></i>Remove all users</button
          >
        </div>
        <div class="col-sm-12 col-md-6 nopadding">
          <input type="text" id="filter-group" ng-model="state.filterGroupMembers" placeholder="Filter..." class="form-control input-sm" />
        </div>
      </rd-widget-taskbar>
      <rd-widget-body classes="no-padding">
        <div class="table-responsive">
          <table class="table table-hover">
            <thead>
              <tr>
                <th>
                  <a ng-click="orderGroupMembers('Username')">
                    Name
                    <span ng-show="sortTypeGroupMembers == 'Username' && !sortReverseGroupMembers" class="glyphicon glyphicon-chevron-down"></span>
                    <span ng-show="sortTypeGroupMembers == 'Username' && sortReverseGroupMembers" class="glyphicon glyphicon-chevron-up"></span>
                  </a>
                </th>
                <th>
                  <a ng-click="orderGroupMembers('TeamRole')">
                    Team Role
                    <span ng-show="sortTypeGroupMembers == 'TeamRole' && !sortReverseGroupMembers" class="glyphicon glyphicon-chevron-down"></span>
                    <span ng-show="sortTypeGroupMembers == 'TeamRole' && sortReverseGroupMembers" class="glyphicon glyphicon-chevron-up"></span>
                  </a>
                </th>
              </tr>
            </thead>
            <tbody>
              <tr
                pagination-id="table2"
                dir-paginate="user in teamMembers | filter:state.filterGroupMembers | orderBy:sortTypeGroupMembers:sortReverseGroupMembers | itemsPerPage: state.pagination_count_groupMembers"
              >
                <td>
                  {{ user.Username }}
                  <span style="margin-left: 5px" ng-if="isAdmin || user.TeamRole === 'Member'">
                    <a ng-click="removeUser(user)"><i class="fa fa-minus-circle space-right" aria-hidden="true"></i>Remove</a>
                  </span>
                </td>
                <td>
                  <i ng-if="user.TeamRole === 'Leader'" class="fa fa-user-plus" aria-hidden="true" style="margin-right: 2px"></i>
                  <i ng-if="user.TeamRole === 'Member'" class="fa fa-user" aria-hidden="true" style="margin-right: 2px"></i>
                  {{ user.TeamRole }}
                  <span style="margin-left: 5px" ng-if="isAdmin">
                    <a style="margin-left: 5px" ng-click="promoteToLeader(user)" ng-if="user.TeamRole === 'Member'"
                      ><i class="fa fa-user-plus space-right" aria-hidden="true"></i>Leader</a
                    >
                    <a style="margin-left: 5px" ng-click="demoteToMember(user)" ng-if="user.TeamRole === 'Leader'"
                      ><i class="fa fa-user-times space-right" aria-hidden="true"></i>Member</a
                    >
                  </span>
                </td>
              </tr>
              <tr ng-if="!teamMembers">
                <td colspan="2" class="text-center text-muted">Loading...</td>
              </tr>
              <tr ng-if="teamMembers.length === 0 || (teamMembers | filter: state.filterGroupMembers).length === 0">
                <td colspan="2" class="text-center text-muted">No team members.</td>
              </tr>
            </tbody>
          </table>
          <div ng-if="teamMembers" class="pull-left pagination-controls">
            <dir-pagination-controls pagination-id="table2"></dir-pagination-controls>
          </div>
        </div>
      </rd-widget-body>
    </rd-widget>
  </div>
</div>
